import request from "@/utils/request";
export interface ICateParams {
  objectId: string;
  cateName: string;
  fatherId: string;
  status: boolean; //控制用户端的分类展示
  children?: ICateParams[];
}
//新增分类
export const categoryPost = (cateObj: ICateParams) => {
  return request.post("classes/ReactCategory", { ...cateObj, status: true });
};

//查询分类
interface CateCondition {
  fatherId?: string;
}
export const categoryGet = (
  where: CateCondition = {
    fatherId: "0-0",
  }
) => {
  return request.get("classes/ReactCategory", {
    params: {
      where,
    },
  });
};

// 课程发布
export interface ICourseType {
  name: string;
  info: string;
  poster: string;
  isvip: boolean;
  category: [string, string];
  catelv1: string;
  catelv2: string;
  desc: string;
}
export const coursePost = (courseObj: ICourseType) => {
  return request.post("classes/ReactAricle", courseObj);
};

//课程查询
export interface ICourseConditionType {
  name?: string;
  isvip?: string | boolean;
  info?: string | { $regex: string; $options: "i" };
  pageSize?: number;
  current?: number;
  keyword?: string;
  created_at?: string;
}
type AttrType = keyof ICourseConditionType;
export const courseGet = (params: ICourseConditionType) => {
  // let where: Partial<ICourseType> = {}; //使用Partial将接口的所有字段变为非必传项
  delete params.current;
  delete params.pageSize;
  delete params.created_at;
  let { isvip, info } = params;
  for (let attr in params) {
    //解决查询条件传递空字符串的问题
    if (params[attr as AttrType] == "") {
      delete params[attr as AttrType];
    }
    // 所有课程
    if (attr == "isvip" && params[attr] == "2") {
      delete params.isvip;
    }
  }
  if (isvip && isvip != "2") {
    isvip = Boolean(Number(isvip));
    params.isvip = isvip;
  }
  if (info) {
    params.info = { $regex: `${params.info}`, $options: "i" }; //模糊查询
  }
  let search = JSON.stringify(params);
  return request.get(`classes/ReactAricle?where=${search}`);
};

//课程修改
export const coursePut = (objectId: string, courseObj: ICourseType) => {
  return request.put(`classes/ReactAricle/${objectId}`, courseObj);
};
